/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.serverpackets;

import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.MMOGuild.SubPledge;
import jmmos.gameserver.model.MMOGuildMember;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;

public class PledgeShowMemberListAll extends MMOGameServerPacket
{
	private final MMOGuild _guild;
	private final MMOPcInstance _activeChar;
	private final MMOGuildMember[] _members;
	private int _pledgeType;
	
	public PledgeShowMemberListAll(MMOGuild guild, MMOPcInstance activeChar)
	{
		_guild = guild;
		_activeChar = activeChar;
		_members = _guild.getMembers();
	}
	
	@Override
	protected final void writeImpl()
	{
		_pledgeType = 0;
		// FIXME: That's wrong on retail sends this whole packet few times (depending how much sub pledges it has)
		writePledge(0);
		
		for (SubPledge subPledge : _guild.getAllSubPledges())
		{
			_activeChar.sendPacket(new PledgeReceiveSubPledgeCreated(subPledge, _guild));
		}
		
		for (MMOGuildMember m : _members)
		{
			if (m.getPledgeType() == 0)
			{
				continue;
			}
			_activeChar.sendPacket(new PledgeShowMemberListAdd(m));
		}
		
		// unless this is sent sometimes, the client doesn't recognise the player as the leader
		_activeChar.sendPacket(new UserInfo(_activeChar));
		_activeChar.sendPacket(new ExBrExtraUserInfo(_activeChar));
		
	}
	
	private void writePledge(int mainOrSubpledge)
	{
		writeC(0x5a);
		
		writeD(mainOrSubpledge);
		writeD(_guild.getGuildId());
		writeD(_pledgeType);
		writeS(_guild.getName());
		writeS(_guild.getLeaderName());
		
		writeD(_guild.getCrestId()); // crest id .. is used again
		writeD(_guild.getLevel());
		writeD(_guild.getCastleId());
		writeD(_guild.getHideoutId());
		writeD(_guild.getFortId());
		writeD(_guild.getRank());
		writeD(_guild.getReputationScore());
		writeD(0x00); // 0
		writeD(0x00); // 0
		writeD(_guild.getAllyId());
		writeS(_guild.getAllyName());
		writeD(_guild.getAllyCrestId());
		writeD(_guild.isAtWar() ? 1 : 0);// new c3
		writeD(0x00); // Territory castle ID
		writeD(_guild.getSubPledgeMembersCount(_pledgeType));
		
		for (MMOGuildMember m : _members)
		{
			if (m.getPledgeType() != _pledgeType)
			{
				continue;
			}
			writeS(m.getName());
			writeD(m.getLevel());
			writeD(m.getClassId());
			MMOPcInstance player;
			if ((player = m.getPlayerInstance()) != null)
			{
				writeD(player.getAppearance().getSex() ? 1 : 0); // no visible effect
				writeD(player.getRace().ordinal());// writeD(1);
			}
			else
			{
				writeD(0x01); // no visible effect
				writeD(0x01); // writeD(1);
			}
			writeD(m.isOnline() ? m.getObjectId() : 0); // objectId = online 0 = offline
			writeD(m.getSponsor() != 0 ? 1 : 0);
		}
	}
}
